home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / libsock / cmp.c < prev    next >
C/C++ Source or Header  |  1997-06-13  |  2KB  |  109 lines

  1. /* cmp.c:  Pilot CMP protocol
  2.  *
  3.  * (c) 1996, Kenneth Albanowski.
  4.  * This is free software, licensed under the GNU Public License V2.
  5.  * See the file COPYING for details.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include "pi-source.h"
  10. #include "pi-socket.h"
  11. #include "pi-padp.h"
  12. #include "pi-cmp.h"
  13. #include "pi-serial.h"
  14.  
  15. int cmp_rx(struct pi_socket *ps, struct cmp * c)
  16. {
  17.   int l;
  18.   unsigned char cmpbuf[10];
  19.   
  20.   Begin(cmp_rx);
  21.   
  22.   if(!ps->rxq)
  23.     ps->serial_read(ps, 200);
  24.   l = padp_rx(ps, cmpbuf, 10);
  25.   
  26.   if( l < 10)
  27.     return -1;
  28.     
  29.   cmp_dump(cmpbuf,0);
  30.             
  31.   c->type = get_byte(cmpbuf);
  32.   c->flags = get_byte(cmpbuf+1);
  33.   c->version = get_short(cmpbuf+2);
  34.   c->reserved = get_short(cmpbuf+4);
  35.   c->baudrate = get_long(cmpbuf+6);
  36.  
  37.   End(cmp_rx);
  38.   
  39.   return 0;
  40. }
  41.  
  42. int cmp_init(struct pi_socket *ps, int baudrate)
  43. {
  44.   unsigned char cmpbuf[10];
  45.  
  46.   set_byte(cmpbuf+0, 2);
  47.   set_long(cmpbuf+2, 0);
  48.   set_long(cmpbuf+6, baudrate);
  49.  
  50.   if(baudrate != 9600) 
  51.         set_byte(cmpbuf+1, 0x80);
  52.   else
  53.         set_byte(cmpbuf+1, 0);
  54.         
  55.   cmp_dump(cmpbuf, 1);
  56.   
  57.   return padp_tx(ps, cmpbuf, 10, padData);
  58. }
  59.  
  60. int cmp_abort(struct pi_socket *ps, int reason)
  61. {
  62.   unsigned char cmpbuf[10];
  63.   
  64.   set_byte(cmpbuf+0, 3);
  65.   set_byte(cmpbuf+1, reason);
  66.   set_long(cmpbuf+2, 0);
  67.   set_long(cmpbuf+6, 0);
  68.  
  69.   cmp_dump(cmpbuf, 1);
  70.   
  71.   return padp_tx(ps, cmpbuf, 10, padData);
  72. }
  73.  
  74. int cmp_wakeup(struct pi_socket *ps, int maxbaud)
  75. {
  76.   unsigned char cmpbuf[200];
  77.   
  78.   set_byte(cmpbuf+0, 1);
  79.   set_byte(cmpbuf+1, 0);
  80.   set_short(cmpbuf+2, CommVersion_1_0);
  81.   set_short(cmpbuf+4, 0);
  82.   set_long(cmpbuf+6, maxbaud);
  83.  
  84.   cmp_dump(cmpbuf, 1);
  85.   
  86.   return padp_tx(ps, cmpbuf, 10, padWake);
  87. }
  88.  
  89. void cmp_dump(unsigned char * cmp, int rxtx)
  90. {
  91. #ifdef DEBUG
  92.  
  93.   fprintf(stderr,"CMP %s %s",
  94.       rxtx ? "TX" : "RX",
  95.       (get_byte(cmp) == 1) ? "WAKE" :
  96.       (get_byte(cmp) == 2) ? "INIT" :
  97.       (get_byte(cmp) == 3) ? "ABRT" : 
  98.       "");
  99.   if((get_byte(cmp) < 1) || (get_byte(cmp) > 3))
  100.       fprintf(stderr, "UNK %d", get_byte(cmp));
  101.   fprintf(stderr,"  Flags: %2.2X Version: %8.8lX Baud: %8.8lX (%ld)\n",
  102.       get_byte(cmp+1),
  103.       get_long(cmp+2),
  104.       get_long(cmp+6),
  105.       get_long(cmp+6));
  106.  
  107. #endif
  108. }
  109.